package cn.iocoder.yudao.module.mqtt.controller.admin.devicealermrecord;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.mqtt.controller.admin.devicealermrecord.vo.*;
import cn.iocoder.yudao.module.mqtt.dal.dataobject.devicealermrecord.DeviceAlermRecordDO;
import cn.iocoder.yudao.module.mqtt.service.devicealermrecord.DeviceAlermRecordService;

@Tag(name = "管理后台 - 报警记录")
@RestController
@RequestMapping("/mqtt/device-alerm-record")
@Validated
public class DeviceAlermRecordController {

    @Resource
    private DeviceAlermRecordService deviceAlermRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建报警记录")
    @PreAuthorize("@ss.hasPermission('mqtt:device-alerm-record:create')")
    public CommonResult<Long> createDeviceAlermRecord(@Valid @RequestBody DeviceAlermRecordSaveReqVO createReqVO) {
        return success(deviceAlermRecordService.createDeviceAlermRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新报警记录")
    @PreAuthorize("@ss.hasPermission('mqtt:device-alerm-record:update')")
    public CommonResult<Boolean> updateDeviceAlermRecord(@Valid @RequestBody DeviceAlermRecordSaveReqVO updateReqVO) {
        deviceAlermRecordService.updateDeviceAlermRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除报警记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mqtt:device-alerm-record:delete')")
    public CommonResult<Boolean> deleteDeviceAlermRecord(@RequestParam("id") Long id) {
        deviceAlermRecordService.deleteDeviceAlermRecord(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除报警记录")
                @PreAuthorize("@ss.hasPermission('mqtt:device-alerm-record:delete')")
    public CommonResult<Boolean> deleteDeviceAlermRecordList(@RequestParam("ids") List<Long> ids) {
        deviceAlermRecordService.deleteDeviceAlermRecordListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得报警记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mqtt:device-alerm-record:query')")
    public CommonResult<DeviceAlermRecordRespVO> getDeviceAlermRecord(@RequestParam("id") Long id) {
        DeviceAlermRecordDO deviceAlermRecord = deviceAlermRecordService.getDeviceAlermRecord(id);
        return success(BeanUtils.toBean(deviceAlermRecord, DeviceAlermRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得报警记录分页")
    @PreAuthorize("@ss.hasPermission('mqtt:device-alerm-record:query')")
    public CommonResult<PageResult<DeviceAlermRecordRespVO>> getDeviceAlermRecordPage(@Valid DeviceAlermRecordPageReqVO pageReqVO) {
        PageResult<DeviceAlermRecordDO> pageResult = deviceAlermRecordService.getDeviceAlermRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeviceAlermRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出报警记录 Excel")
    @PreAuthorize("@ss.hasPermission('mqtt:device-alerm-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDeviceAlermRecordExcel(@Valid DeviceAlermRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeviceAlermRecordDO> list = deviceAlermRecordService.getDeviceAlermRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "报警记录.xls", "数据", DeviceAlermRecordRespVO.class,
                        BeanUtils.toBean(list, DeviceAlermRecordRespVO.class));
    }

}